<?php

/**
 * This is the model class for table "{{working_process}}".
 *
 * The followings are the available columns in table '{{working_process}}':
 * @property integer $id
 * @property integer $profile_id
 * @property integer $related_id
 * @property integer $language
 * @property string $working_time
 * @property string $company
 * @property string $job_description
 * @property string $basic_salary
 * @property string $real_salary
 * @property string $create_date
 * @property string $created_by
 * @property string $update_date
 * @property string $updated_by
 *
 * The followings are the available model relations:
 * @property Profile $profile
 */
class WorkingProcess extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return WorkingProcess the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{working_process}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('profile_id', 'required'),
			array('profile_id', 'numerical', 'integerOnly'=>true),
			array('working_time, created_by, updated_by', 'length', 'max'=>50, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('company', 'length', 'max'=>100, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('job_description', 'length', 'max'=>300, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('basic_salary, real_salary', 'length', 'max'=>20, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('create_date, update_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, profile_id, working_time, company, job_description, basic_salary, real_salary, create_date, created_by, update_date, updated_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'profile' => array(self::BELONGS_TO, 'Profile', 'profile_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => t('LBL_ID', 'workingProcess'),
			'profile_id' => 'Profile',
			'working_time' => t('LBL_WORKING_TIME', 'workingProcess'),
			'company' => t('LBL_COMPANY', 'workingProcess'),
			'job_description' => t('LBL_JOB_DESCRIPTION', 'workingProcess'),
			'basic_salary' => t('LBL_BASIC_SALARY', 'workingProcess'),
			'real_salary' => t('LBL_REAL_SALARY', 'workingProcess'),
			'create_date' => t('LBL_CREATE_DATE', 'workingProcess'),
			'created_by' => t('LBL_CREATED_BY', 'workingProcess'),
			'update_date' => t('LBL_UPDATE_DATE', 'workingProcess'),
			'updated_by' => t('LBL_UPDATED_BY', 'workingProcess'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('profile_id',$this->profile_id);
		$criteria->compare('working_time',$this->working_time,true);
		$criteria->compare('company',$this->company,true);
		$criteria->compare('job_description',$this->job_description,true);
		$criteria->compare('basic_salary',$this->basic_salary,true);
		$criteria->compare('real_salary',$this->real_salary,true);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('created_by',$this->created_by,true);
		$criteria->compare('update_date',$this->update_date,true);
		$criteria->compare('updated_by',$this->updated_by,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function beforeSave() {
            if (parent::beforeSave()) {

                $current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);

                //Get user is logging
                $logedInUser = User::model()->findByPk(Yii::app()->user->id);	   

                $this->update_date = $current_date;

                if (isset($logedInUser)) {
                    $this->updated_by = $logedInUser->username;
                }	                            

                $this->setIsNewRecord(false);

                if (isset($this->yearOfBirth) && isset($this->monthOfBirth) && isset($this->dayOfBirth) &&
                $this->yearOfBirth != "" && $this->monthOfBirth != "" && $this->dayOfBirth != "" ){
                    $this->birth_date = $this->yearOfBirth . "-" . $this->monthOfBirth . "-" . $this->dayOfBirth;            
                } 

                return true;
            } else {
                return false;
            }
        }
}